package com.wang.exam.controller;

import com.wang.exam.pojo.Grade;
import com.wang.exam.pojo.Teacher;
import com.wang.exam.service.MajorCoursesService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class MajorCoursesController {
    @Autowired
    MajorCoursesService majorCoursesService;

    @RequestMapping("/majorsCoursesList")
    @ResponseBody
    public Object getmajorsCoursesList(int page , int rows){
        Map<String,Object> map  = new HashMap<>();
        int n = majorCoursesService.getCount();
        map.put("rows",majorCoursesService.getMajorCourses(page,rows));
        map.put("total",n);
        return map;
    }
    @RequestMapping("/add_major_course")
    @ResponseBody
    public Object addMajorCourse(@RequestParam("major_id")String major_id,
                             @RequestParam("course_id")String course_id,
                             @RequestParam("course_name")String course_name
    ) {
        Map<String, String> map = new HashMap<String, String>();
        Map<String, Object> addmap = new HashMap<String, Object>();
        addmap.put("major_id",major_id);
        addmap.put("course_id",course_id);
        addmap.put("course_name",course_name);
        try {
            majorCoursesService.addMajorCourse(addmap);
            map.put("code", "200");
            map.put("msg", "添加成功！");
            return map;
        } catch (Exception e) {
            map.put("msg", "添加失败！");
            return map;
        }
    }
    @RequestMapping("/up_major_course")
    @ResponseBody
    public Object updateMajorCourse(@RequestParam("major_id")String major_id,
                                 @RequestParam("course_id")String course_id,
                                 @RequestParam("course_name")String course_name
    ) {
        Map<String, String> map = new HashMap<String, String>();
        Map<String, Object> addmap = new HashMap<String, Object>();
        addmap.put("major_id",major_id);
        addmap.put("course_id",course_id);
        addmap.put("course_name",course_name);
        try {
            majorCoursesService.upMajorCourses(addmap);
            map.put("code", "200");
            map.put("msg", "修改成功！");
            return map;
        } catch (Exception e) {
            map.put("msg", "修改失败！");
            return map;
        }
    }
    @RequestMapping("/de_major_course")
    @ResponseBody
    public Object deleteMajorCourse(@RequestParam("major_id")String major_id,
                                 @RequestParam("course_id")String course_id
    ) {
        Map<String, String> map = new HashMap<String, String>();
        Map<String, Object> addmap = new HashMap<String, Object>();
        addmap.put("major_id",major_id);
        addmap.put("course_id",course_id);
        try {
            majorCoursesService.deleteMajorCourse(addmap);
            map.put("code", "200");
            map.put("msg", "删除成功！");
            return map;
        } catch (Exception e) {
            map.put("msg", "删除失败！");
            return map;
        }
    }


}
